<template>
  <div>
    <div class="list" v-if="list.length">
      <div class="item" v-for="(item, index) in list" :key="index" @click="onItem(item)">
        <el-image class="img g-img-placeholder" :src="item.url" fit="cover" v-if="imageTypes.includes(item.type)" />
        <i class="el-icon-video-camera" v-else-if="videoTypes.includes(item.type)" />
      </div>
    </div>
    <el-image ref="previewRef" class="hide" :src="previewSrc" :preview-src-list="previewSrcList" />
    <VideoPlayer ref="videoPlayer" />
  </div>
</template>
<script>
// 多媒体列表。用于展示图片和视频，及其预览和播放
import { imageTypes, videoTypes } from '@/utils/baseData'
import VideoPlayer from '@/views/statement/components/DialogVideoPlayer'
export default {
  components: { VideoPlayer },
  props: { list: { type: Array, default: () => [] } },
  data() {
    return {
      imageTypes,
      videoTypes,
      previewSrcList: [],
      previewSrc: ''
    }
  },
  methods: {
    onItem(item) {
      // 预览大图
      if (imageTypes.includes(item.type)) {
        this.previewSrcList = this.list.filter(i => imageTypes.includes(i.type)).map(i => i.url)
        this.previewSrc = item.url
        this.$nextTick(() => this.$refs.previewRef?.clickHandler())
        return
      }
      // 播放视频
      if (videoTypes.includes(item.type)) {
        return this.$refs.videoPlayer.play(item.url)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.list {
  display: flex;
  flex-wrap: wrap;
}
.item {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 64px;
  height: 64px;
  border-radius: 4px;
  border: 1px solid #c0ccda;
  box-sizing: border-box;
  overflow: hidden;
  color: #e6a23c;
  font-size: 32px;
  margin: 0 5px 5px 0;
  cursor: pointer;
}
.img {
  width: 100%;
  height: 100%;
}
</style>
